/**
 * HorizontalLetterAssembler.java | Created on 18 09 2010
 */
package com.pacm.assembler;

import com.pacm.letter.Letter;

/**
 * The horizontal letter assembler implementation
 *
 * @author Paul Augustine C. Mendoza
 */
public class HorizontalLetterAssembler implements LetterAssembler {

    private static final String LINE_FEED = "\n";

    private StringBuffer stringBuffer;
    private int size;

    public HorizontalLetterAssembler() {
        stringBuffer = new StringBuffer();
    }

    public void assemble(Letter[] letters, int size) {
        this.size = size;
        populateStringBuffer(letters);
    }

    public String getPrintableString() {
        return this.stringBuffer.toString();
    }

    private void populateStringBuffer(Letter[] letters) {
        for (int i = 0; i < this.size; i++) {
            processRow(letters, i);
            if (!isLastIndex(i)) {
                this.stringBuffer.append(LINE_FEED);
            }
        }
    }

    private void processRow(Letter[] letters, int rowIndex) {
        String[] row;
        for (Letter letter :letters) {
            row = letter.getMatrix()[rowIndex];
            processColumns(row);
        }
    }

    private boolean isLastIndex(int index) {
        return index == this.size - 1;
    }

    private void processColumns(String[] row) {
        for (String item : row) {
            this.stringBuffer.append(item);
        }
    }
}
